﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using LY.Entity;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;
using LY.Entity.Jsons;
using LY.Interface;
namespace QJ.CMS.Areas.Admin.Controllers
{
    public class SysTemplateController : Controller
    {
        //
        // GET: /Admin/SysTemplate/


         private static ISysTemplateService _syservice;
         public SysTemplateController(ISysTemplateService lSysservice)
        {
            _syservice = lSysservice;
        }
        public ActionResult Index()
        {
            return View();
        }
        //模板列表信息详细显示
        [HttpPost]
        public ActionResult SysTemplateList(SysTemplateSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<S_SysTemplate> lList = _syservice.QuerySysTemplateList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<S_SysTemplate>(lList, colkey, colsinfo.Split(','));
            return Json(at);
        }
        //TemplateEdit
        //添加
        /// <summary>
        /// 链接新增或编辑操作，返回不同状态界面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult TemplateEdit(int id=0)
        {
            var Template = new S_SysTemplate();
            if (id>0)
            {
                Template = _syservice.GetSysTemplate(id);
            }             
            return View(Template);
        }
        //保存新增或编辑链接信息
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult TemplateSave(S_SysTemplate sys, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            { 
                int ret = _syservice.SaveTemplate(sys);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        //删除链接
        [HttpPost]
        public JsonResult TemplateDelete(int id)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                }

                int ret = _syservice.DeleteSysTemplate(id);


                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {

                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }



    }
}
